<?php


namespace app\api\controller\v34;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\MonthFortune;
use app\common\cachemodel\YearFortune;
use app\common\lib\Util;
use app\common\model\Article as ArticleModel;
use app\common\model\Recode as RecodeModel;

class Article extends Auth2
{
    protected $colors = [
        0 => ['name' => '困难', 'color' => '#7775ff',],
        1 => ['name' => '警戒', 'color' => '#ffa536',],
        2 => ['name' => '平平', 'color' => '#ffed17',],
        3 => ['name' => '吉利', 'color' => '#00f4e7',],
        4 => ['name' => '大吉', 'color' => '#00e9a0',],
    ];

    public function fortune_detail()
    {
        $in_sign = input('in_sign/d', 0);
        //若用户登陆了且有档案则，获取用户的落座
        if (!$in_sign && $this->user && $this->user['rid'])
        {
            $map = ['uid' => $this->user['id'], 'isself' => 1];
            $recode_info = (new RecodeModel())->getSelfRecode($map);

            if ((!$recode_info || !$recode_info['sun']))
            {
                return show(0, '档案缺失请添加档案');
            }
            $in_sign = explode('-', $recode_info['sun'])[0];
        } elseif (!in_array($in_sign, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], true))
        {
            return show(0, '星座未知');
        }
        $weekDay = [
            '星期日',
            '星期一',
            '星期二',
            '星期三',
            '星期四',
            '星期五',
            '星期六',
        ];

        $rid = $this->user && $this->user['rid'] ? $this->user['rid'] : 0;
        $day = (new ArticleModel())->getFortune('day', $in_sign);
        $day['time_range'] = date('Y.m.d ') . $weekDay[date('w')];
        $week = (new ArticleModel())->getFortune('week', $in_sign);
        $week['time_range'] = date('Y.m.d', strtotime($week['publish_time'])) . '-' . date('Y.m.d', strtotime($week['publish_time'] . ' +6 day'));
        $month = (new ArticleModel())->getFortune('month', $in_sign);
        $month['time_range'] = date('Y.m.01', strtotime($month['publish_time'])) . '-' . date('m.d', strtotime($month['publish_time'] . ' +1 month -1 day'));
        $year = (new ArticleModel())->getFortune('year', $in_sign);
        $year['time_range'] = date('Y.01.01', strtotime($year['publish_time'])) . '-12.31';

        if ($rid)
        {
            $day['btn_data'] = null;
            $week['btn_data'] = null;
            // 判断本月月运是否已解锁 没解锁则添加按钮
            //获取用户已解锁的月运数据
            $unlockMonths = (new MonthFortune())->getReocdeFortuneUnlockMonths($rid);
            $nowMonthData = date('Y-m');
            if (!in_array($nowMonthData, $unlockMonths, true))
            {   // 未解锁
                $month['btn_data'] = [
                    'name' => '前往解锁个人专属月运',
                    'target' => 'local',
                    'link' => "fortune_for_month?rid={$rid}&time={$nowMonthData}&need_auth=1",
                ];
            } else
            {
                $month['btn_data'] = null;
            }
            // 判断今年年运是否已解锁 没解锁则添加按钮
            //获取用户已解锁数据
            $unlockYears = (new YearFortune())->getReocdeFortuneUnlockYears($rid);
            $unlockYears = array_map(function ($timeValue)
            {
                return (string)($timeValue);
            }, $unlockYears);
            $nowYear = date('Y');
            if (!in_array(date('Y'), $unlockYears, true))
            {  // 未解锁
                $year['btn_data'] = [
                    'name' => '前往解锁个人专属年运',
                    'target' => 'local',
                    'link' => "fortune_for_year?rid={$rid}&time={$nowYear}&need_auth=1",
                ];
            } else
            {
                $year['btn_data'] = null;
            }
        } else
        {
            $btnData = [
                'name' => Util::IsHuaweiAndOppo() ? '添加档案了解个人专属提醒' : '添加档案了解个人专属运势',
                'target' => 'local',
                'link' => 'app_tab?type=0&need_auth=1',
            ];
            $day['btn_data'] = $btnData;
            $week['btn_data'] = $btnData;
            $month['btn_data'] = $btnData;
            $year['btn_data'] = $btnData;
        }

        $fortuneData = [
            'fortune_day' => $day,
            'fortune_week' => $week,
            'fortune_month' => $month,
            'fortune_year' => $year
        ];
        $resData = [
            'luck_adversity' => $this->colors,
        ];

        $isShowToday = Util::CheckAppVersion('2.10.4') === '小';
        foreach ($fortuneData as $key => $fortuneItem)
        {
            if ($key !== 'fortune_day')
            {
                $scoreData = [
                    [
                        'name' => '整体',
                        'score' => $fortuneItem['whole'] ?? 0,
                    ],
                    [
                        'name' => '感情',
                        'score' => $fortuneItem['love'] ?? 0,
                    ],
                    [
                        'name' => '财富',
                        'score' => $fortuneItem['money'] ?? 0,
                    ],
                    [
                        'name' => '事业',
                        'score' => $fortuneItem['career'] ?? 0,
                    ],
                    [
                        'name' => '健康',
                        'score' => $fortuneItem['health'] ?? 0,
                    ]
                ];
                $scoreList = [];
                foreach ($scoreData as $datum)
                {
                    $resScore = Util::getScorePercentInfo2($datum['score']);
                    $resScore['name'] = $datum['name'];
                    $scoreList[] = $resScore;
                }
                $isShow = true;
            } else
            {
                $isShow = $isShowToday;
            }

            if ($isShow)
            {
                $resData[$key] = [
                    'title' => $fortuneItem['introduction'],
                    'introduction' => $fortuneItem['time_range'],
                    'content' => $fortuneItem['content'],
                    'score_list' => $scoreList ?? null,
                    'btn_data' => $fortuneItem['btn_data']
                ];
            }
        }

        return show(config('code.success'), '请求成功', $resData);
    }
}
